from django.db import models
from django.contrib.auth.models import AbstractUser
import random
import string
# Create your models here.
class UserModel(AbstractUser):
    mobile= models.CharField(max_length=11,unique=True,verbose_name="手机号")
    avatar = models.ImageField(upload_to='avatar/',verbose_name="头像")
    nikename = models.CharField(max_length=50, default="", null=True, verbose_name='用户昵称')


    # 登录自动生成用户昵称
    def save(self, *args, **kwargs):
        if not self.nikename:
            self.nikename = self.generate_nikename()
        super().save(*args, **kwargs)
    @staticmethod
    def generate_nikename():
        first="".join(random.sample(string.ascii_letters, 3))
        middle = "_"
        backend=''.join(random.sample(string.ascii_letters + string.digits, 8))
        nikename = f"{first}{middle}{backend}"
        return nikename
    print(nikename)
    class Meta:
        db_table = 'user'
        verbose_name_plural = "用户信息表"
    def __str__(self):
        return self.mobile
